package com.corundumstudio.socketio.demo;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

/**
 * Created by Chejianer on 2016/10/27.
 */
public class PushChatLauncher {
    public static void main(String[] args) throws InterruptedException {
        final Map<String,PushChatObject> pushChatLauncherMap=new LinkedHashMap<String, PushChatObject>();
        final Map<String,PushChatObject> failPushMessage=new LinkedHashMap<String, PushChatObject>();
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        final SocketIOServer server = new SocketIOServer(config);
        server.addEventListener("pushEvent", PushChatObject.class, new DataListener<PushChatObject>() {
            @Override
            public void onData(SocketIOClient client, PushChatObject data, AckRequest ackRequest) {
                // broadcast messages to all clients
               // server.getBroadcastOperations().sendEvent("chatevent", data);
                //data.setSessionId(client.getSessionId().toString());
                if(pushChatLauncherMap.get(data.getClientId())!=null){
                    try{
                        server.getClient(pushChatLauncherMap.get(data.getClientId()).getSessionId()).sendEvent("pushEvent",data);
                      //  server.getClient(client.getSessionId()).sendEvent("pushEvent",data);
                        client.sendEvent("pushEvent",data);
                    }catch(NullPointerException e){
                       // e.printStackTrace();
                        failPushMessage.put(data.getClientId(),data);
                    }

                }


            }
        });
        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(SocketIOClient socketIOClient) {

                PushChatObject data=new PushChatObject();
                data.setSessionId(socketIOClient.getSessionId());
                data.setClientId(socketIOClient.getHandshakeData().getSingleUrlParam("clientId"));
                pushChatLauncherMap.put(data.getClientId(),data);
                server.getClient(socketIOClient.getSessionId()).sendEvent("connectEvent",data);
                if(failPushMessage.containsKey(data.getClientId())){
                    String clientId=data.getClientId();
                    pushChatLauncherMap.get(clientId).setMessage(failPushMessage.get(clientId).getMessage());
                    pushChatLauncherMap.get(clientId).setUserName(failPushMessage.get(clientId).getUserName());
                    server.getClient(pushChatLauncherMap.get(clientId).getSessionId()).sendEvent("pushEvent",pushChatLauncherMap.get(clientId));
                    failPushMessage.remove(data.getClientId());
                }
            }
        });

        server.start();

        Thread.sleep(Integer.MAX_VALUE);

        server.stop();
    }

}
